{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CREATE INDEX IF NOT EXISTS FOR (n:Node) ON (n.name)\n"
     ]
    }
   ],
   "source": [
    "from neo4j import GraphDatabase\n",
    "from python_nlp.kg.neo4j.config_wsl_tmp import NEO4J_CONFIG_PSYCHO\n",
    "    \n",
    "# 驱动\n",
    "driver = GraphDatabase.driver(**NEO4J_CONFIG_PSYCHO)\n",
    "\n",
    "# 加索引和约束\n",
    "with driver.session() as ss:\n",
    "    for query in [\n",
    "        \"CREATE INDEX IF NOT EXISTS FOR (n:Node) ON (n.name)\",\n",
    "    ]:\n",
    "        print(query)\n",
    "        ss.run(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def new_node(name, label=\"Node\"):\n",
    "    with driver.session() as ss:\n",
    "        cypher = f\"\"\"\\\n",
    "            MERGE (n:Node:{label} {{name: {repr(name)}}})\n",
    "            RETURN n\n",
    "            \"\"\"\n",
    "        print(cypher)\n",
    "        record = ss.run(cypher)\n",
    "        print(record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def new_rel(name1, name2, rel_label):\n",
    "    with driver.session() as ss:\n",
    "        cypher = f\"\"\"\\\n",
    "            MATCH (n1:Node {{name: {repr(name1)}}})\n",
    "            MATCH (n2:Node {{name: {repr(name2)}}})\n",
    "            MERGE (n1)-[r:{rel_label}]->(n2)\n",
    "            RETURN n1, r, n2\n",
    "            \"\"\"\n",
    "        print(cypher)\n",
    "        record = ss.run(cypher)\n",
    "        print(record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            MERGE (n:Node:Positive {name: 'Will'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c99bb50>\n",
      "            MERGE (n:Node:Positive {name: '约束'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c9d86a0>\n",
      "            MERGE (n:Node:Positive {name: '兴趣'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4b8160>\n",
      "            MERGE (n:Node:Positive {name: '内啡肽'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3291f0>\n",
      "            MERGE (n:Node:Positive {name: '工作学习'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3369a0>\n",
      "            MERGE (n:Node:Positive {name: '锻炼'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c336190>\n",
      "            MERGE (n:Node:Negative {name: 'Lx活动'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4c9fa0>\n",
      "            MERGE (n:Node:Negative {name: 'Sleepy'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4c92b0>\n",
      "            MERGE (n:Node:Negative {name: 'Lx瘾'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3c0f10>\n",
      "            MERGE (n:Node:Negative {name: '疲劳困难'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c36f490>\n",
      "            MERGE (n:Node:Neutral {name: 'Body'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3c02e0>\n",
      "            MERGE (n:Node:Neutral {name: '娱乐'})\n",
      "            RETURN n\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4c9430>\n"
     ]
    }
   ],
   "source": [
    "xmap_nodes = {\n",
    "    'Positive': [\n",
    "        'Will', '约束',\n",
    "        '兴趣', '内啡肽',\n",
    "        '工作学习', '锻炼',\n",
    "    ],\n",
    "    \n",
    "    'Negative': [\n",
    "        'Lx活动',\n",
    "        'Sleepy', 'Lx瘾',\n",
    "        '疲劳困难',\n",
    "    ],\n",
    "    \n",
    "    'Neutral': [\n",
    "        'Body',\n",
    "        '娱乐',\n",
    "    ],\n",
    "}\n",
    "for label, names in xmap_nodes.items():\n",
    "    for name in names:\n",
    "        new_node(name, label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            MATCH (n1:Node {name: 'Body'})\n",
      "            MATCH (n2:Node {name: 'Lx瘾'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4bca60>\n",
      "            MATCH (n1:Node {name: 'Lx瘾'})\n",
      "            MATCH (n2:Node {name: 'Lx活动'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4c9c70>\n",
      "            MATCH (n1:Node {name: 'Lx活动'})\n",
      "            MATCH (n2:Node {name: 'Sleepy'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c36fdf0>\n",
      "            MATCH (n1:Node {name: '疲劳困难'})\n",
      "            MATCH (n2:Node {name: 'Sleepy'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c428b50>\n",
      "            MATCH (n1:Node {name: '疲劳困难'})\n",
      "            MATCH (n2:Node {name: 'Lx瘾'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c33e130>\n",
      "            MATCH (n1:Node {name: '工作学习'})\n",
      "            MATCH (n2:Node {name: '疲劳困难'})\n",
      "            MERGE (n1)-[r:熵增]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c336310>\n",
      "            MATCH (n1:Node {name: '约束'})\n",
      "            MATCH (n2:Node {name: '工作学习'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c336a90>\n",
      "            MATCH (n1:Node {name: '兴趣'})\n",
      "            MATCH (n2:Node {name: 'Will'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3c01f0>\n",
      "            MATCH (n1:Node {name: 'Will'})\n",
      "            MATCH (n2:Node {name: '工作学习'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c36f610>\n",
      "            MATCH (n1:Node {name: 'Will'})\n",
      "            MATCH (n2:Node {name: '锻炼'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4c9dc0>\n",
      "            MATCH (n1:Node {name: '锻炼'})\n",
      "            MATCH (n2:Node {name: 'Will'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c4bc100>\n",
      "            MATCH (n1:Node {name: 'Body'})\n",
      "            MATCH (n2:Node {name: '工作学习'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c33ed90>\n",
      "            MATCH (n1:Node {name: '锻炼'})\n",
      "            MATCH (n2:Node {name: 'Body'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c33ef70>\n",
      "            MATCH (n1:Node {name: '娱乐'})\n",
      "            MATCH (n2:Node {name: 'Lx瘾'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3406a0>\n",
      "            MATCH (n1:Node {name: '工作学习'})\n",
      "            MATCH (n2:Node {name: '兴趣'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c331be0>\n",
      "            MATCH (n1:Node {name: '工作学习'})\n",
      "            MATCH (n2:Node {name: '内啡肽'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c336850>\n",
      "            MATCH (n1:Node {name: '内啡肽'})\n",
      "            MATCH (n2:Node {name: 'Will'})\n",
      "            MERGE (n1)-[r:正反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3361f0>\n",
      "            MATCH (n1:Node {name: 'Sleepy'})\n",
      "            MATCH (n2:Node {name: '工作学习'})\n",
      "            MERGE (n1)-[r:负反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3506d0>\n",
      "            MATCH (n1:Node {name: '娱乐'})\n",
      "            MATCH (n2:Node {name: '疲劳困难'})\n",
      "            MERGE (n1)-[r:负反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c3366a0>\n",
      "            MATCH (n1:Node {name: 'Lx活动'})\n",
      "            MATCH (n2:Node {name: 'Body'})\n",
      "            MERGE (n1)-[r:负反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c331820>\n",
      "            MATCH (n1:Node {name: '兴趣'})\n",
      "            MATCH (n2:Node {name: 'Lx瘾'})\n",
      "            MERGE (n1)-[r:负反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c340dc0>\n",
      "            MATCH (n1:Node {name: '内啡肽'})\n",
      "            MATCH (n2:Node {name: 'Lx瘾'})\n",
      "            MERGE (n1)-[r:负反馈]->(n2)\n",
      "            RETURN n1, r, n2\n",
      "            \n",
      "<neo4j._sync.work.result.Result object at 0x7f5b2c340f70>\n"
     ]
    }
   ],
   "source": [
    "xmap_rel = {\n",
    "    '熵增': [\n",
    "        ('Body', 'Lx瘾'),\n",
    "        ('Lx瘾', 'Lx活动'),\n",
    "        ('Lx活动', 'Sleepy'),\n",
    "        ('疲劳困难', 'Sleepy'),\n",
    "        ('疲劳困难', 'Lx瘾'),\n",
    "        ('工作学习', '疲劳困难'),\n",
    "    ],\n",
    "    '正反馈': [\n",
    "        ('约束', '工作学习'),\n",
    "        ('兴趣', 'Will'),\n",
    "        ('Will', '工作学习'),\n",
    "        ('Will', '锻炼'),\n",
    "        ('锻炼', 'Will'),\n",
    "        ('Body', '工作学习'),\n",
    "        ('锻炼', 'Body'),\n",
    "        ('娱乐', 'Lx瘾'),\n",
    "        ('工作学习', '兴趣'),\n",
    "        ('工作学习', '内啡肽'),\n",
    "        ('内啡肽', 'Will'),\n",
    "    ],\n",
    "    '负反馈': [\n",
    "        ('Sleepy', '工作学习'),\n",
    "        ('娱乐', '疲劳困难'),\n",
    "        ('Lx活动', 'Body'),\n",
    "        ('兴趣', 'Lx瘾'),\n",
    "        ('内啡肽', 'Lx瘾'),\n",
    "    ]\n",
    "}\n",
    "for label, rels in xmap_rel.items():\n",
    "    for rel in rels:\n",
    "        n1, n2 = rel\n",
    "        new_rel(n1, n2, label)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "p38pt212cu118LCCC",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
